<template>
    <el-dialog :title="!dataForm.id ? '批量关联项目' : '修改'" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()">
        
        <el-form-item label="关联项目" :label-width="formLabelWidth" prop="projectId">
          <el-select v-model="dataForm.projectId" filterable placeholder="请选择关联项目" @change="projectChange">
            <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联送货司机" :label-width="formLabelWidth" prop="driverId">
          <el-select v-model="dataForm.driverId" filterable placeholder="请选择关联送货司机">
            <el-option v-for="item in driverList" :key="item.id" :label="item.driverName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
          <el-button size="small" type="primary" @click="dataFormSubmit()" :disabled="isDisabled">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </template>
  
  <script>
  import { v4 as uuidv4 } from "uuid";
  import { getToken } from "@/utils/auth";
  export default {
    emits: ["refreshDataList"],
    data() {
      return {
        dialogFormVisible: false,
        isDisabled: false,
        dataForm: {
          driverId: '',//关联送货司机
          projectId: '',//关联项目
        },
        ids:[],
        projectList: [],
        driverList: [],
        fileList: [],
        url: process.env.VUE_APP_BASE_API,
        headers: {
          token: getToken() //从cookie里获取token，并赋值  Authorization ，而不是token
        },
        dataRule: {
          driverName: [
            { required: true, message: '司机姓名必填' },
          ],
          driverPhone: [
            { required: true, message: '司机联系电话必填' },
          ],
          licenseNumber: [
            { required: true, message: '车牌号必须填写' },
          ]
        },
        formLabelWidth: "140px",
      };
    },
    created() { },
    methods: {
      async init(ids) {
        this.ids=ids
        this.dialogFormVisible = true;
        this.$nextTick(async () => {
          this.$refs["dataForm"].resetFields();
          this.fileList = []
          this.projectList = []
          this.driverList = []
          await this.Sevice({
            url: "/project/list",
            method: "get",
          }).then(async (res) => {
            if (res.code != 200) {
            } else {
              this.projectList = res.list
            }
          });
          // if (!this.dataForm.id) {
          //   // 新增
          //   // this.treeListTreeSetCurrentNode()
          // } else {
          //   //获取详情
          //   //获取详情
          //   this.Sevice({
          //     url: "/product/info/" + this.dataForm.id,
          //     method: "get",
          //   }).then(async (res) => {
          //     // console.log(res);
          //     if (res.code != 200) {
          //       // this.$message.error(res.data.msg)
          //     } else {
          //       this.dataForm.driverId = res.info.driverId;
          //       this.dataForm.projectId = res.info.projectId;
          //       this.getDriver(res.info.projectId)
          //     }
          //   });
          // }
        })
  
      },
      projectChange(id) {
        this.getDriver(id)
      },
      //获取司机
      getDriver(id) {
        this.Sevice({
          url: "/project/info/" + id,
          method: "get",
        }).then(async (res) => {
          // console.log(res);
          if (res.code != 200) {
            // this.$message.error(res.data.msg)
          } else {
            this.driverList = res.info.projectDrivers;
          }
        });
      },
      // 表单提交
      async dataFormSubmit() {
        this.$refs["dataForm"].validate(async (valid) => {
          if (valid) {
            for(let i=0;i<this.ids.length;i++){
                this.ids[i].driverId=this.dataForm.driverId
                this.ids[i].projectId=this.dataForm.projectId
            }
            let url = "/product/updateProject";
            this.isDisabled = true;
            await this.Sevice({
              url: url,
              method: "post",
              data: this.ids
            }).then((res) => {
              if (res && res.code == 200) {
                this.dialogFormVisible = false;
                this.$message({
                  message: res.msg,
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.isDisabled = false;
                    this.$emit("getUserlist");
                  },
                });
              } else {
                this.isDisabled = false;
              }
            }).catch(error => {
              console.log(error)
              this.isDisabled = false;;
            });
          }
        });
      },
    },
  };
  </script>
  
  <style scoped>
  .mod-menu .menu-list__input>.el-input__inner,
  .mod-menu .icon-list__input>.el-input__inner {
    cursor: pointer;
  }
  
  .mod-menu__icon-popover {
    width: 458px;
    overflow: hidden;
  }
  
  .mod-menu__icon-inner {
    width: 478px;
    max-height: 258px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  
  .mod-menu__icon-list {
    width: 458px;
    padding: 0;
    margin: -8px 0 0 -8px;
  }
  
  .mod-menu__icon-list>.el-button {
    padding: 8px;
    margin: 8px 0 0 8px;
  }
  
  .mod-menu__icon-list>.el-button>span {
    display: inline-block;
    vertical-align: middle;
    width: 18px;
    height: 18px;
    font-size: 18px;
  }
  
  .mod-menu .icon-list__tips {
    font-size: 18px;
    text-align: center;
    color: #e6a23c;
    cursor: pointer;
  }
  
  .el-tree {
    height: 200px;
    overflow-y: auto;
  }
  </style>
  